<?php
namespace Home\Controller;
use Think\Controller;

class AccountController extends CommonController {
	//验证码图片
  public function code(){
			$Verify = new \Think\Verify();
			$Verify->fontSize =  20;    // 大小  
			$Verify->length   =    4;    // 个数   
			$Verify->useNoise =   false; // 有无点啥的	
			$Verify->codeSet = '0123456789'; //设置为纯数字	
			$Verify->entry();
	}
	//关联账号显示
	public function relevance(){
		$Mod = M('users');
		$userMod = $Mod ->field('main_user_id,bd_user_id')-> where('user_id="%s"',$_SESSION['users']['user_id']) ->find();		
		if(!empty($userMod['bd_user_id'])){
			$bd_user_id = explode(',',$_SESSION['users']['bd_user_id']);
			unset($bd_user_id[0]);
			foreach($bd_user_id as $v){
				$bd_user[] = $Mod -> field('user_name')->where("user_id = '%d' ",$v)->find();
			}
			//关联账户的信息			
			$this ->assign('bd_user',$bd_user);
		}elseif(!empty($userMod['main_user_id'])){		
			
			$bd_user = $Mod -> field('user_name')->where("main_user_id = '%s' ", $userMod['main_user_id'])->select();
			$main_user_id = ltrim($userMod['main_user_id'],',');
			
			$main_user[] = $Mod -> field('user_name')->where("user_id = '%s' ", $main_user_id)->find();
			
			foreach($bd_user as $k=>$v){
				if($v['user_name']==$_SESSION['users']['user_name']){
						unset($v['user_name']);
				}
				
				if(!empty($v)){
					$main_user[] = $Mod -> field('user_name')->where("user_name = '%s' ",$v['user_name'])->find();
				}
			}
			
			$this ->assign('main_user',$main_user);
		}		
		
		$this -> display('');
	}

	//切换账号
	public function target(){
		//获得需要切换到的用户名
		$user_name = I('get.user_name');
		$userMod = M('users');	

		$user = $userMod ->where("user_name = '%s' ",$user_name)->find();
		$_SESSION['users'] = $user;
		$this -> redirect('home/account/relevance');
	}

	//解除绑定
	public function jiechu(){

		$userMod = M('users');
		$userMod -> startTrans();	
		if(!empty($_SESSION['users']['bd_user_id'])){
			//绑定用户的信息
			$bd_user_id = explode(',',$_SESSION['users']['bd_user_id']);
			//去掉数组中空值
			unset($bd_user_id[0]);			
			//找到被绑定的信息			
			$jiechu_bd_user_id = $userMod ->where("user_name = '%s' ",I('get.user_name'))->find();
			$jiechu_bd_user_id['main_user_id'] = '';
			//去掉绑定人信息中的被绑定人的id
			foreach($bd_user_id as $k=>$v){
				if($jiechu_bd_user_id['user_id']==$v){
					unset($bd_user_id[$k]);
				}
			}

			if(!empty($bd_user_id)){
				$bd_user_id = ','.implode($bd_user_id,',');
				$bd_user_id = rtrim($bd_user_id);
			}else{
				$bd_user_id = '';
			}

			$_SESSION['users']['bd_user_id']=$bd_user_id;


			$res1 = $userMod -> where("user_id = '%s' ",$_SESSION['users']['user_id'])->save($_SESSION['users']);
			$res2 = $userMod -> where("user_id = '%s' ",$jiechu_bd_user_id['user_id'])->save($jiechu_bd_user_id);
			if($res1>0&&$res2>0){
				$userMod -> commit();
				$this->success('解绑成功',U('home/account/relevance'));
			}else{
				$userMod -> rollback();
				$this->success('解绑失败',U('home/account/relevance'));
			}
			
		}else{
			//获取的是被绑定用户id
			$main_user_id = explode(',',$_SESSION['users']['main_user_id']);
			$main_user_id = $main_user_id[1];
			//找到绑定用户信息
			$bd_user = $userMod ->where("user_id = '%d' ",$main_user_id)->find();
			//去掉绑定人信息中的被绑定人的id
			$bd_user_id = explode(',',$bd_user['bd_user_id']);
			unset($bd_user_id[0]);
			foreach($bd_user_id as $k=>$v){
				if($_SESSION['users']['user_id']==$v){
					unset($bd_user_id[$k]);
				}
			}
			$bd_user_id = ','.implode($bd_user_id,',');
			$bd_user_id = rtrim($bd_user_id,',');

			$bd_user['bd_user_id'] = $bd_user_id;
			//将被绑定人的主id清空			
			$_SESSION['users']['main_user_id'] = '';
			$res1 = $userMod -> where("user_id = '%s' ",$_SESSION['users']['user_id'])->save($_SESSION['users']);
			$res2 = $userMod -> where("user_id = '%s' ",$main_user_id)->save($bd_user);
			if($res1>0&&$res2>0){
				$userMod -> commit();
				$this->success('解绑成功',U('home/account/relevance'));
			}else{
				$userMod -> rollback();
				$this->success('解绑失败',U('home/account/relevance'));
			}
		}		
		
	}

	//添加绑定账号
	public function addBangdingAjax(){

		$userMod = M('users');		
		$user = $userMod -> where('user_id='.I('post.userid')) -> find();
		//获取验证码
    	$Verify = new \Think\Verify();
		$res = $Verify->check(I('post.code'));
		if($res){
			if($user){
				if($user['repassword']==md5(I('post.aqpassword'))){
					if(empty($user['ec_salt'])){
						if($user['password']==md5(I('post.password'))){
								if($user['main_user_id']||$user['bd_user_id']){
									echo '22222';
								}else{
									echo '1';
								}
						}else{
							echo '2222';
						}
					}else{
						if($user['password']==md5(md5(I('post.password')).$user['ec_salt'])){
							if($user['main_user_id']||$user['bd_user_id']){
									echo '22222';
								}else{
									echo '1';
								}
						}else{
							echo '2222';
						}
					}
				}else{
					echo '222';
				}
			}else{
				echo '22';
			}
		}else{
			echo '2';
		}
		
	}
	//添加绑定
	public function addaccount(){
		//开启事务
		$userMod = M('users');
		$userMod -> startTrans();		
		$main_user = $userMod -> where("user_id = '%s' ",I('post.userid'))->find();
		$main_user['main_user_id'] = ','.$_SESSION['users']['user_id'];
		if(empty($_SESSION['users']['bd_user_id'])){
			$_SESSION['users']['bd_user_id'] = ','.I('post.userid');
		}else{
			$_SESSION['users']['bd_user_id'] =$_SESSION['users']['bd_user_id'].','.I('post.userid');			
		}
		$res1 =  $userMod -> where("user_id = '%s' ",I('post.userid'))->save($main_user);
		$res2 =  $userMod -> where("user_id = '%s' ",$_SESSION['users']['user_id'])->save($_SESSION['users']);
		if($res1>0 && $res2>0){
			$userMod -> commit();
			$this->success('添加绑定成功',U('home/account/relevance'));
		}else{
			$userMod -> rollback();
			$this->success('添加绑定成功',U('home/account/relevance'));
		}
	}




}